<?php

namespace app\index\controller\wanlshop;

use app\common\controller\Wanlshop;
use think\Db;

/**
 * 主页
 * @internal
 */
class Console extends Wanlshop
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    
    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function index()
    {
		$shop_id = $this->shop->id;
        $date      = $this->request->post('date', date('Y-m-d 00:00:00',time()-6*86400).' - '.date('Y-m-d 23:59:59'));
        list($start, $end) = explode(' - ', $date);

        $start = strtotime($start);
        $end = strtotime($end);
        $data      = $this->getOrderStatisticsData($date);
		$paylist = $createlist = $uvlist = $orderlist = $userlist = [];
        $sum_uv = 0;
        $pay_sum = 0;
        $pay_num = 0;
        $create_num = 0;
        foreach ($data as $day)
        {
            $starttime   = strtotime($day['starttime']);
            $endtime     = strtotime($day['endtime']);
            $columns[]   = $day['column'];
            // 订单数
            $create_num += $createlist[] = $create = model('app\index\model\wanlshop\Order')
                ->where('createtime','between', [$starttime,$endtime])
                ->where(['shop_id' => $shop_id])
                ->count();
            // 成交数
            $pay_num += $paylist[] = $pay =  model('app\index\model\wanlshop\Order')
                ->where('paymenttime','between', [$starttime,$endtime])
                ->where('state','in', [2,3,4,5,6])
                ->where(['shop_id' => $shop_id])
                ->count();
            //浏览量
            $sum_uv += $uvlist[] = Db::name('wanlshop_shop_uv')
                ->where('createtime','between', [$starttime,$endtime])
                ->where(['shop_id' => $shop_id])
                ->count();
            //订单金额
            $pay_sum += $orderlist[] = model('app\index\model\wanlshop\Pay')
                ->where('updatetime','between', [$starttime,$endtime])
                ->where('pay_state','in', [1])
                ->where(['shop_id' => $shop_id])
                ->sum('price');
            //新增用户量
            $userlist[] = model('app\index\model\User')
                ->where('jointime','between', [$starttime,$endtime])
                ->where(['shop_id' => $shop_id])
                ->count();
            //订单转化率
            $convertlist[] = $create ? bcdiv($pay , $create,2) * 100  : 0;
        }
        //总浏览量
        $uv['sum_uv'] = $sum_uv;
        //PC端浏览量
        $uv['pc_uv'] = Db::name('wanlshop_shop_uv')
            ->where('createtime','between', [$start,$end])
            ->where(['shop_id' => $shop_id])
            ->where(['platform' => 'pc'])
            ->count();
        //移动端浏览量
        $uv['wap_uv'] = Db::name('wanlshop_shop_uv')
            ->where('createtime','between', [$start,$end])
            ->where(['shop_id' => $shop_id])
            ->where(['platform' => 'wap'])
            ->count();
        //订单总金额
        $order['pay_sum'] = $pay_sum;
        $order['convert_rate'] = $create_num ? bcdiv($pay_sum , $create_num,2) * 100 . '%' : '0%';

        $statistics = [
            'columns'   => $columns,
            'createlist' => $createlist,
            'paylist' => $paylist,
            'uv' => $uv,
            'uvlist' => $uvlist,
            'order' => $order,
            'orderlist' => $orderlist,
            'userlist' => $userlist,
            'convertlist' => $convertlist,
        ];
        if ($this->request->isPost()) {
            $this->success('', '', $statistics);
        }

		
		//七日增长
		$week = model('app\index\model\wanlshop\Order')->whereTime('createtime', 'week')->where('shop_id', $shop_id)->count();
		$lastweek = model('app\index\model\wanlshop\Order')->whereTime('createtime', 'last week')->where('shop_id', $shop_id)->count();
		if($week != 0 && $lastweek != 0 ){
			$sevendnu = bcdiv(bcmul(100, bcsub($week, $lastweek, 2), 2), $lastweek, 2);
		}else{
			$sevendnu = 0;
		}
		$limit = 20;
		//会员购买次数排行
        $ranking['buylist'] = model('app\index\model\wanlshop\Order')
            ->with('user')
            ->field('count(*) as count')
            ->where('state','in', [2,3,4,5,6])
            ->where(['order.shop_id' => $shop_id])
            ->group('user_id')
            ->order('count desc')
            ->limit($limit)
            ->select();

        //会员购物金额排行
        $ranking['paylist'] = model('app\index\model\wanlshop\Order')
            ->with(['user','pay'])
            ->field('sum(pay.price) as sumprice')
            ->where('state','in', [2,3,4,5,6])
            ->where(['order.shop_id' => $shop_id])
            ->group('user_id')
            ->order('sumprice desc')
            ->limit($limit)
            ->select();

        //会员活跃度排行
        $ranking['uvlist'] = Db::name('wanlshop_shop_uv')
            ->alias('uv')
            ->join('user us','uv.user_id = us.id')
            ->field('count(*) as count,us.nickname,us.avatar,us.mobile')
            ->where('uv.shop_id',$shop_id)
            ->group('uv.user_id')
            ->limit($limit)
            ->select();

        //商品访问量排行
        $ranking['viewslist'] = model('app\index\model\wanlshop\Goods')
            ->where(['shop_id' => $shop_id])
            ->order('views desc')
            ->limit($limit)
            ->select();

        //产品销售排行
        $ranking['saleslist'] = model('app\index\model\wanlshop\Goods')
            ->where(['shop_id' => $shop_id])
            ->order('sales desc')
            ->limit($limit)
            ->select();
        //收藏排行
        $ranking['likelist'] = model('app\index\model\wanlshop\Goods')
            ->where(['shop_id' => $shop_id])
            ->order('like desc')
            ->limit($limit)
            ->select();
        $this->view->assign([
            'totaluser'        => model('app\index\model\wanlshop\Goods')->where('shop_id', $shop_id)->count(), //商品总数
            'totalviews'       => model('app\index\model\wanlshop\Goods')->where('shop_id', $shop_id)->sum('views'), //总访问数
            'totalorder'       => model('app\index\model\wanlshop\Order')->where('shop_id', $shop_id)->count(), // 总订单数
            'totalorderamount' => model('app\index\model\wanlshop\Pay')->where('shop_id', $shop_id)->sum('price'), //总金额
            'todayuserlogin'   => model('app\api\model\wanlshop\Cart')->where('shop_id', $shop_id)->count(), // 加购物车
            'todayusersignup'  => model('app\index\model\wanlshop\ShopFollow')->where('shop_id', $shop_id)->count(), // 店铺收藏
            'todayorder'       => model('app\index\model\wanlshop\Order')->whereTime('createtime', 'today')->where('shop_id', $shop_id)->count(), // 今日订单数量
            'unsettleorder'    => model('app\index\model\wanlshop\Order')->whereTime('createtime', 'today')->where(['shop_id' => $shop_id, 'state' => 2])->count(), // 未处理数量
            'sevendnu'         => $sevendnu.'%', 
            'sevendau'         => model('app\index\model\wanlshop\Pay')->whereTime('createtime', 'today')->where('shop_id', $shop_id)->sum('price'), //今日销售额
            'date' => $date,
            'ranking' => $ranking,
        ]);
        //首次渲染图表数据
        $this->assignconfig('statistics', $statistics);
        return $this->view->fetch();
    }



}


